﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using WebClient;
namespace WebClient.admin.Modules.userGroups
{
    public partial class List : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.btnDelete.Attributes.Add("onclick", "a=confirm('Đồng ý xóa?');document.getElementById('" + hidDelete.ClientID + "').value=a;");
            this.btnDuyet.Attributes.Add("onclick", "a=confirm('Đồng ý duyệt?');document.getElementById('" + this.Duyet.ClientID + "').value=a");

            if (!this.IsPostBack)
            {
                LoadDataGrid();
            }
        }

        private void LoadDataGrid()
        {

            WebData db = new WebData();
            this.grdDs.DataSource = db.GetAllUsergroups();
            this.grdDs.DataBind();
        }


        private void Active()
        {
            try
            {
                int n = this.grdDs.Rows.Count;
                if (this.Duyet.Value == "true")
                {
                    bool isselected = false;
                    if (n > 0)
                    {
                        CheckBox chk;
                        for (int i = 0; i < n; i++)
                        {
                            chk = (CheckBox)this.grdDs.Rows[i].FindControl("chkActive");
                            try
                            {
                                // int _ID = Convert.ToInt32(this.grdDs.DataKeys[this.grdDs.Rows[i].RowIndex].Value);
                                WebData db = new WebData();
                                Usergroups item = new Usergroups();
                                //item.IdNick = _ID;
                                // item.IsActive = chk.Checked == true ? 1 : 0;

                                if (db.OnSubmitUsergroups(item, 3))
                                    isselected = true;
                            }
                            catch
                            {

                            }
                        }
                    }
                    if (isselected)
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + Message.Update + "');", true);
                        LoadDataGrid();
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + Message.Update_err + "');", true);
                    }
                }
            }
            catch { }

        }

        private void Delete()
        {

            try
            {
                int n = this.grdDs.Rows.Count;
                if (this.hidDelete.Value == "true")
                {
                    bool isselected = false;
                    if (n > 0)
                    {
                        CheckBox chk;
                        for (int i = 0; i < n; i++)
                        {
                            chk = (CheckBox)this.grdDs.Rows[i].FindControl("chkDelete");
                            try
                            {
                                if (chk.Checked)
                                {
                                    // int _ID = Convert.ToInt32(this.grdDs.DataKeys[this.grdDs.Rows[i].RowIndex].Value);
                                    WebData db = new WebData();
                                    Usergroups item = new Usergroups();
                                    if (db.OnSubmitUsergroups(item, 2))
                                        isselected = true;
                                }

                            }
                            catch
                            {

                            }
                        }
                    }
                    if (isselected)
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + Message.Delete + "');", true);
                        LoadDataGrid();
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + Message.Delete_err + "');", true);
                    }
                }
            }
            catch { }

        }

        protected void grdDs_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


        protected void btnDuyet_Click(object sender, ImageClickEventArgs e)
        {
            Active();
        }

        protected void btnimgDel_Click(object sender, ImageClickEventArgs e)
        {
            Delete();

        }
    }
}